package com.frcdi.qms.equipment.service.impl;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.equipment.domain.EquipmentType;
import com.frcdi.qms.equipment.mapper.EquipmentTypeMapper;
import com.frcdi.qms.equipment.service.IEquipmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备类型Service业务层处理
 *
 * @author cell
 * @date 2023-10-10
 */
@Service
public class EquipmentTypeServiceImpl implements IEquipmentTypeService
{
    @Autowired
    private EquipmentTypeMapper equipmentTypeMapper;

    /**
     * 查询设备类型
     *
     * @param typeId 设备类型主键
     * @return 设备类型
     */
    @Override
    public EquipmentType selectEquipmentTypeByTypeId(Long typeId)
    {
        return equipmentTypeMapper.selectEquipmentTypeByTypeId(typeId);
    }

    /**
     * 查询设备类型列表
     *
     * @param equipmentType 设备类型
     * @return 设备类型
     */
    @Override
    public List<EquipmentType> selectEquipmentTypeList(EquipmentType equipmentType)
    {
        return equipmentTypeMapper.selectEquipmentTypeList(equipmentType);
    }

    /**
     * 新增设备类型
     *
     * @param equipmentType 设备类型
     * @return 结果
     */
    @Override
    public int insertEquipmentType(EquipmentType equipmentType)
    {
        EquipmentType info = equipmentTypeMapper.selectEquipmentTypeByTypeId(equipmentType.getParentId());
        // 填充修改父级列表数据
        equipmentType.setAncestors(info.getAncestors() + "," + equipmentType.getParentId());
        int row = equipmentTypeMapper.insertEquipmentType(equipmentType);
        return row;
    }

    /**
     * 修改设备类型
     *
     * @param equipmentType 设备类型
     * @return 结果
     */
    @Override
    public int updateEquipmentType(EquipmentType equipmentType)
    {
        EquipmentType newParent = equipmentTypeMapper.selectEquipmentTypeByTypeId(equipmentType.getParentId());
        EquipmentType oldresult = equipmentTypeMapper.selectEquipmentTypeByTypeId(equipmentType.getTypeId());
        if (StringUtils.isNotNull(newParent) && StringUtils.isNotNull(oldresult))
        {
            String newAncestors = newParent.getAncestors() + "," + newParent.getTypeId();
            String oldAncestors = oldresult.getAncestors();
            equipmentType.setAncestors(newAncestors);
            updateMaterialTypChildren(equipmentType.getTypeId(), newAncestors, oldAncestors);
        }
        int row = equipmentTypeMapper.updateEquipmentType(equipmentType);
        return row;
    }

    /**
     * 批量删除设备类型
     *
     * @param typeIds 需要删除的设备类型主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentTypeByTypeIds(Long[] typeIds)
    {
        return equipmentTypeMapper.deleteEquipmentTypeByTypeIds(typeIds);
    }

    /**
     * 删除设备类型信息
     *
     * @param typeId 设备类型主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentTypeByTypeId(Long typeId)
    {
        return equipmentTypeMapper.deleteEquipmentTypeByTypeId(typeId);
    }

    /**
     * 修改子元素关系
     *
     * @param id 被修改的物料类型ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    private void updateMaterialTypChildren(Long id, String newAncestors, String oldAncestors)
    {
        List<EquipmentType> children = equipmentTypeMapper.selectChildrenId(id);
        for (EquipmentType child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            equipmentTypeMapper.updateEquipmentTypeChildren(children);
        }
    }

    /**
     * 设备类型名称是否唯一
     *
     * @param equipmentType 设备类型信息
     * @return 结果
     */
    @Override
    public boolean checkEquipmentTypeNameUnique(EquipmentType equipmentType) {
        Long typeId = StringUtils.isNull(equipmentType.getTypeId()) ? -1L : equipmentType.getTypeId();
        EquipmentType info = equipmentTypeMapper.checkEquipmentTypeNameUnique(equipmentType.getEquipmentTypeName(), equipmentType.getParentId());
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 是否存在设备类型子节点
     *
     * @param typeId 设备类型ID
     * @return 结果
     */
    @Override
    public boolean hasChildById(Long typeId) {
        int result = equipmentTypeMapper.hasChildById(typeId);
        return result > 0;
    }
}
